package com.wowangz.cms.common.api;
 
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.service.CommonMagnifierService;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.ServletUtils;

/**
 * 
 * 公共放大镜Controller
 * 
 * @author leiyang3
 *
 */
@Scope("prototype")
@Controller
public class CommonMagnifierController extends BaseController {
	
	/**
	 * 通用放大镜
	 * @param arg0
	 * @param arg1
	 * @param model
	 * @param writer
	 * @throws ControllerException
	 */
	@RequestMapping(value = "/publicMagnifier/publicMagnifier.json", method = RequestMethod.POST)
	public void publicMagnifier(HttpServletRequest arg0,HttpServletResponse arg1, Model model, Writer writer) {

		PangingParam pangingParam = null;
		DataResult dataResult =null;
		DataGrid dataGrid = null;

		HashMap queryParam=null;
		
		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(arg0,PangingParam.class);
			queryParam = ServletUtils.getParametersToHashMap(arg0);
            
			//回调的sql
			String callSqlKey=(String)queryParam.get("callSqlKey");
			
			//所有列的field名称
			List columnList=new ArrayList();
			if(queryParam!=null){
				Iterator iter = queryParam.entrySet().iterator();
				while (iter.hasNext())
				{
					Map.Entry entry = (Map.Entry) iter.next();
					Object key = entry.getKey();
					Object value = entry.getValue();
					String columnField=key.toString();
					if(columnField.startsWith("column_data_")){
					columnList.add(columnField);
					}
			    }
			}

			// 设置查询参数
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);
			
			queryParam.put("startrow", dataGrid.getStartrow());
			queryParam.put("endrow", dataGrid.getEndrow());
			queryParam.put("rows", dataGrid.getRows());
			queryParam.put("sort", dataGrid.getSort());
			
			CommonMagnifierService commonMagnifierService = (CommonMagnifierService) IssTransactionContainer.getInstance(CommonMagnifierService.class, false);
			String countKey = callSqlKey + "_count";
			String pageKey = callSqlKey + "_page";
			// 获取查询结果
			long resultCount = commonMagnifierService.findResultCount(countKey,queryParam);
			List resultList = commonMagnifierService.findResultList(pageKey,queryParam);

			// 获取配置页面样式扣的结果
			dataResult = new DataResult();
			dataResult.setTotal(resultCount);
			dataResult.setRows(resultList);

			arg1.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
